package ca.uwindsor.cs.deepweb.estimation.experiment;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;

public abstract class DuplicateDocumentIndexer {

	/**
	 * The file to create duplicated document index
	 */
	protected File file;

	/**
	 * The writer to write the index to a directory Specified by indexDir
	 * 
	 * @see #indexDir
	 */
	protected IndexWriter indexWriter;

	/**
	 * The directory restore the index files
	 */
	protected String indexDir;

	protected String content;

	protected String title;

	protected int size;

	/**
	 * @param f
	 * @param dir
	 * @param s
	 */
	public DuplicateDocumentIndexer(File f, String dir, int s) {
		// TODO Auto-generated constructor stub
		file = f;
		indexDir = dir;
		size = s;
	}

	protected boolean isQualified() {
		return true;
	}

	protected boolean processDocument() {
		return false;
	}

	public void generateIndex() {
		if (!processDocument()) {
			System.exit(-1);
			System.out.println("error");
		}
		Analyzer luceneAnalyzer = new StandardAnalyzer();
		// IndexWriter indexWriter;
		try {
			indexWriter = new IndexWriter(indexDir, luceneAnalyzer);
			int i;
			for (i = 1; i <= size; i++) {
				Document document = new Document();
				Field FieldPath = new Field(
						FilesContentIndexer.FIELD_INTERNAL_ID, Integer
								.toString(i), Field.Store.YES,
						Field.Index.UN_TOKENIZED);
				Field FieldTitle = new Field(FilesContentIndexer.FIELD_TITLE,
						title, Field.Store.YES, Field.Index.UN_TOKENIZED);
				Field FieldBody = new Field(FilesContentIndexer.FIELD_CONTENT,
						content, Field.Store.NO, Field.Index.TOKENIZED,
						Field.TermVector.YES);
				document.add(FieldPath);
				document.add(FieldTitle);
				document.add(FieldBody);
				indexWriter.addDocument(document);
			}
			System.out
					.println("index is completed! \n Start to optimize the index...");
			indexWriter.optimize();
			System.out.println("optimization is completed!");
			indexWriter.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getContentSize() {
		return content.length();
	}

}
